using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Sunlit.House.Seconds.Dto;
using Abp.Extensions;
using Sunlit.House.Authorization;

namespace Sunlit.House.Seconds
{
    public class SecondHandAppService: AsyncCrudAppService<SecondHand,SecondHandDto,long,GetAllSecondHandInput,
        CreateSecondHandInput,UpdateSecondHandInput>,ISecondHandAppService
    {
        public SecondHandAppService(IRepository<SecondHand,long> repository):base(repository)
        {
            CreatePermissionName = PermissionNames.Pages_SecondHand_Create;
            UpdatePermissionName = PermissionNames.Pages_SecondHand_Edit;
            DeletePermissionName = PermissionNames.Pages_SecondHand_Delete;
        }
        protected override IQueryable<SecondHand> CreateFilteredQuery(GetAllSecondHandInput input)
        {
            return base.CreateFilteredQuery(input)
                .Include(s => s.Region)
                .Include(s => s.SecondHandImages)
                .Include(s=>s.Village)
                .WhereIf(!input.Title.IsNullOrWhiteSpace(), s => s.Title.Contains(input.Title)|| s.Village.Name.Contains(input.Title))
                .WhereIf(input.RegionId.HasValue, s => s.RegionId == input.RegionId)
                .WhereIf(input.ShowOnHome.HasValue, s => s.ShowOnHome == input.ShowOnHome)
                .WhereIf(input.InSlider.HasValue, s => s.InSlider == input.InSlider)
                .WhereIf(input.CityId.HasValue, s => s.Region.CityId == input.CityId);
        }
        public override async Task<SecondHandDto> GetAsync(EntityDto<long> input)
        {
            var entity = await Repository.GetAll()
                .Include(e => e.Village)
                .Include(e => e.Region)
                .Include(e=>e.SecondHandImages)
                .FirstOrDefaultAsync(e => e.Id == input.Id);
            return ObjectMapper.Map<SecondHandDto>(entity);
        }
        public async Task<SecondHandDto> CreateExAsync(CreateSecondHandExInput input)
        {
            CheckCreatePermission();
            SecondHand hand = new();
            ObjectMapper.Map(input.SecondHand, hand);
            if (input.ImageUrls !=null)
            {
                hand.SecondHandImages = new List<SecondHandImage>();
                foreach (var item in input.ImageUrls)
                {
                    hand.SecondHandImages.Add(new SecondHandImage
                    {
                        ImageUrl = item.Trim(),
                        Title = hand.Title
                    });
                }
            }
            var inserted = await Repository.InsertAsync(hand);
            return ObjectMapper.Map<SecondHandDto>(inserted);
        }
        public async Task InSlider(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.InSlider = true;
        }
        public async Task OutSlider(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.InSlider = false;
        }
        public async Task ShowOnHome(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.ShowOnHome = true;
        }
        public async Task HideOnHome(EntityDto<long> input)
        {
            var entity = await Repository.GetAsync(input.Id);
            entity.ShowOnHome = false;
        }
    }
}
